
package org.esisar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.esisar.jdbc.JDBCConnector;
import org.esisar.model.Model;

public class JDBCDAO {

	private Connection connection;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;



	public void addToDB(StatementCreator statementCreator, Model model){

		try {

			connection = JDBCConnector.JDBCConnector.connect();
			preparedStatement = statementCreator.addStatement(connection, model);
			preparedStatement.executeUpdate();
			connection.commit();

		} catch (SQLException e) {
			try {
				rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteFromDB(StatementCreator statementCreator, List<Long> keys){
		try {

			connection = JDBCConnector.JDBCConnector.connect();
			preparedStatement = statementCreator.deleteStatement(connection, keys);
			preparedStatement.executeUpdate();


		} catch (SQLException e) {
			try {
				rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}


	public void updateDB(StatementCreator statementCreator, Model model){

		try {

			connection = JDBCConnector.JDBCConnector.connect();
			preparedStatement = statementCreator.updateStatement(connection, model);
			preparedStatement.executeUpdate();

			connection.commit();

		} catch (SQLException e) {
			try {
				rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public List<Model> findAll(StatementCreator statementCreator, String field, String value) throws SQLException{
		List<Model> result = Collections.emptyList();

		connection = JDBCConnector.JDBCConnector.connect();
		preparedStatement = statementCreator.readStatement(connection, field, value);
		ResultSet rs = preparedStatement.executeQuery();
		
		List<Model> models = new ArrayList<Model>();
		
		while(rs.next()){
			Model model = statementCreator.fillModel(rs);
			
			models.add(model);
		}

		return models;
	}

	public Model find(StatementCreator statementCreator, String field, String value) throws SQLException{

		try {

			connection = JDBCConnector.JDBCConnector.connect();
			preparedStatement = statementCreator.readStatement(connection, field, value);
			ResultSet rs = preparedStatement.executeQuery();

			Model model = null;
			
			while(rs.next()){
				model = statementCreator.fillModel(rs);
			}
		
			connection.commit();
			
			return model;
			
		} catch (SQLException e) {
			try {
				rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		throw new SQLException();
	}



	private void rollback() throws SQLException{
		if (connection != null){
			connection.rollback();
		}
	}

	private void close() throws SQLException{
		if (preparedStatement != null){
			preparedStatement.close();
		}

		if (connection != null){
			connection.close();
		}

	}



}
